<?php
class Exception_RequestFormatInvalidate extends QException {} 
class cmshopapiclient_base extends Helper_interface_base
{
	public $AuthToken; //用户 token
	public $verb; // 应用 API 名,写在 API 接口中
	public $xmlns='xmlns="cmshop:apis:shopapi"'; //
	function __construct(){
		$this->_loadconfig();
	}
	function _loadconfig(){
	   $this->config=array(
          'APP_NAME'=>'9884a-56f4bc-00b204e-6014f4de',
          'CERT_NAME'=>'708b4498-a839d7203-d4c4e7c664-32c6c7fabc',
          'url'=>'http://www.17008.com/index.php/shopapi',
	   	  'host'=>'www.17008.com',
		  'port'=>'80',
		  'path'=>'/index.php/shopapi',
		  'tokenUrl'=>'http://www.17008.com/index.php/shopapi/token/sessionid/'

        );
	}
	/***
	 * 发送类 
	 * 	 $sendXmlArr : 数据 数组 ,会被组织成 Xml 
	 * 	 	如果 本来就是 字符串的 ,就认为本来就是  Xml 	 
	 * 	 $returnXml : 返回 值是 数组 或 原生 xml .默认 是 数组 
	 */
	function sendHttpRequest($sendXmlArr,$isreturnXml=0){
		// 1,body 部分  
		if(is_array($sendXmlArr)){
			$requestBody=Helper_xml::simpleArr2xml($sendXmlArr);
		}elseif(is_string($sendXmlArr)){
			$requestBody=$sendXmlArr;
		}else{
			return false;
		}

		//if() socket 发送
		// 2, header 部分 ,Ebay 需要的 Hearder
		$headers='';
		$headers .='CMSTORE-API-APP-NAME: ' . $this->config["APP_NAME"]."\r\n";
		$headers .='CMSTORE-API-CERT-NAME: ' . $this->config["CERT_NAME"]."\r\n";
		$headers .='CMSTORE-API-CALL-NAME: ' . $this->verb."\r\n";
		// 3,发送
		$this->contenttype='text/xml';
		$result=$this->socket_post($this->config["url"],$requestBody,$headers,'utf-8',30);
		$response=$result['body'];

		//返回 数组
//		$this->_last_response_array=Helper_xml::xmlparse($response);
// 		if($isreturnXml){
			return $response;
// 		}else{
// 			return Helper_xml::xmlparse($response);
// 		}
	}
	
	function dateTime($timestamp=null){
		return gmdate('Y-m-d\TH:i:s.Z\Z',$timestamp);
	}
	/**
	 * 发送请求
	 *
	 * @param boolean $returnXml 是否返回源xml，默认返回数组
	 * @return array|xml
	 */
	function sendRequest($xmlArr,$returnXml=0){
	    if ($xmlArr instanceof SimpleXMLElement ){
	        $xmlArr['requestXML']=$xmlArr;
	    }elseif(is_array($xmlArr)){
    	    
	    }
	    if($this->AuthToken){
            $xmlArr['RequesterCredentials']=array(
					'AuthToken'=>$this->AuthToken,
				);
        }
	    $xmlArr=array(
	        $this->verb.'Request '.$this->xmlns=>$xmlArr,
	    );
	    $xmlArr=Helper_xml::simpleArr2xml($xmlArr);
	    return $this->sendHttpRequest($xmlArr,$returnXml);
	}
	function responseIsWarming($responseArr=null){
	    if (is_null($responseArr)){
	        $responseArr=$this->_last_response_array;
	    }
	    return $responseArr['Ack'] =='Warming';
	}
	function responseIsFailure($responseArr=null){
	    if (is_null($responseArr)){
	        $responseArr=$this->_last_response_array;
	    }
	    return $responseArr['Ack'] =='Failure';
	}
	function responseIsSuccess($responseArr=null){
	    if (is_null($responseArr)){
	        $responseArr=$this->_last_response_array;
	    }
	    return $responseArr['Ack'] =='Success';
	}
	function responseErrorMsg(){
	    $errors=$this->_last_response_array['Errors'];
	    if (isset($errors['LongMessage'])) {
            return $errors['LongMessage'];
        } else {
            $errorMsg='';
            foreach ($errors as $e) {
                if ($e['SeverityCode'] == 'Error') {
                    $errorMsg .= $e['LongMessage'] . ',';
                }
            }
            return $errorMsg;
        }
	}
}